package com.open.capacity.nk.service.impl;

import com.open.capacity.db.config.util.DataSourceHolder;
import com.open.capacity.db.config.util.DataSourceKey;
import com.open.capacity.nk.constant.NKDataSourceKeys;
import com.open.capacity.nk.entity.AaPerson;
import com.open.capacity.nk.entity.AaPerson;
import com.open.capacity.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.nk.entity.AaPerson;
import com.open.capacity.nk.dao.AaPersonDao;
import com.open.capacity.nk.service.AaPersonService;


@Service
public class AaPersonServiceImpl implements AaPersonService {

    @Autowired
    private AaPersonDao aaPersonDao;

    @Override
    public int syncData() {
        Map<String, Object> params = new HashMap<>();
        List<AaPerson> list = new ArrayList<>();

        //本院
        DataSourceHolder.setDataSourceKey(DataSourceKey.nk00);
        List<AaPerson> list0 = aaPersonDao.findAll(params);
        list0.forEach(t->t.setIdorganization(NKDataSourceKeys.CENTER.getCode()));
        list.addAll(list0);

//        //天河
//        DataSourceHolder.setDataSourceKey(DataSourceKey.nk01);
//        List<AaPerson> list1 = aaPersonDao.findAll(params);
//        list1.forEach(t->t.setIdorganization(NKDataSourceKeys.TH.getCode()));
//        list.addAll(list1);
//
//        //海珠
//        DataSourceHolder.setDataSourceKey(DataSourceKey.nk02);
//        List<AaPerson> list2 = aaPersonDao.findAll(params);
//        list2.forEach(t->t.setIdorganization(NKDataSourceKeys.HZ.getCode()));
//        list.addAll(list2);
//
//        //越秀
//        DataSourceHolder.setDataSourceKey(DataSourceKey.nk03);
//        List<AaPerson> list3 = aaPersonDao.findAll(params);
//        list3.forEach(t->t.setIdorganization(NKDataSourceKeys.YX.getCode()));
//        list.addAll(list3);
//
//        //荔湾
//        DataSourceHolder.setDataSourceKey(DataSourceKey.nk04);
//        List<AaPerson> list4 = aaPersonDao.findAll(params);
//        list4.forEach(t->t.setIdorganization(NKDataSourceKeys.LW.getCode()));
//        list.addAll(list4);
//
//        //白云
//        DataSourceHolder.setDataSourceKey(DataSourceKey.nk05);
//        List<AaPerson> list5 = aaPersonDao.findAll(params);
//        list5.forEach(t->t.setIdorganization(NKDataSourceKeys.BY.getCode()));
//        list.addAll(list5);
//
//        //番禺
//        DataSourceHolder.setDataSourceKey(DataSourceKey.nk06);
//        List<AaPerson> list6 = aaPersonDao.findAll(params);
//        list6.forEach(t->t.setIdorganization(NKDataSourceKeys.PY.getCode()));
//        list.addAll(list6);
//
//        //黄埔
//        DataSourceHolder.setDataSourceKey(DataSourceKey.nk07);
//        List<AaPerson> list7 = aaPersonDao.findAll(params);
//        list7.forEach(t->t.setIdorganization(NKDataSourceKeys.HP.getCode()));
//        list.addAll(list7);
//
//        //南沙
//        DataSourceHolder.setDataSourceKey(DataSourceKey.nk08);
//        List<AaPerson> list8 = aaPersonDao.findAll(params);
//        list8.forEach(t->t.setIdorganization(NKDataSourceKeys.NS.getCode()));
//        list.addAll(list8);
//
//        //花都
//        DataSourceHolder.setDataSourceKey(DataSourceKey.nk09);
//        List<AaPerson> list9 = aaPersonDao.findAll(params);
//        list9.forEach(t->t.setIdorganization(NKDataSourceKeys.HD.getCode()));
//        list.addAll(list9);
//
//        //增城
//        DataSourceHolder.setDataSourceKey(DataSourceKey.nk10);
//        List<AaPerson> list10 = aaPersonDao.findAll(params);
//        list10.forEach(t->t.setIdorganization(NKDataSourceKeys.ZC.getCode()));
//        list.addAll(list10);
//
//        //从化
//        DataSourceHolder.setDataSourceKey(DataSourceKey.nk11);
//        List<AaPerson> list11 = aaPersonDao.findAll(params);
//        list11.forEach(t->t.setIdorganization(NKDataSourceKeys.CH.getCode()));
//        list.addAll(list11);

        DataSourceHolder.clearDataSourceKey();

        //本地存库
        aaPersonDao.delete();
        list.forEach(t -> {
            aaPersonDao.save(t);
        });

        return list.size();
    }

    /**
     * 添加
     *
     * @param aaPerson
     */
    public int save(AaPerson aaPerson) {
        return aaPersonDao.save(aaPerson);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return aaPersonDao.delete();
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public AaPerson findById(String id) {
        return aaPersonDao.findById(id);
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<AaPerson> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<AaPerson> list = aaPersonDao.findAll(params);
        PageInfo<AaPerson> pageInfo = new PageInfo(list);

        return PageResult.<AaPerson>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<AaPerson> findAll(Map<String, Object> params) {
        return aaPersonDao.findAll(params);
    }

    @Override
    public Integer countByDepartment(String companyId, String departmentId,String departmentName) {
        Map<String, Object> param = new HashMap<>();
        param.put("companyId", companyId);
        if(StringUtil.isNotEmpty(departmentId)) {
            param.put("departmentId", departmentId);
        }
        if(StringUtil.isNotEmpty(departmentName)) {
            param.put("departmentName", departmentName);
        }
        List<AaPerson> list = findAll(param);
        return list.size();
    }

    @Override
    public Integer countByCompany(String companyId) {
        Map<String, Object> param = new HashMap<>();
        param.put("companyId", companyId);
        List<AaPerson> list = findAll(param);
        return list.size();
    }
}
